package com.order.controller.back_end;

import com.order.dao.Dish;
import com.order.dao.Result;
import com.order.dao.User;
import com.order.service.DishService;
import com.order.service.DishTypeservice;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Scope("prototype")
@AllArgsConstructor
@CrossOrigin
public class DishController {

    @Resource
    private DishService dishService;
    @Resource
    private DishTypeservice dishTypeService;

/*    @ResponseBody
    @RequestMapping("/dish_findDishTypes")
    public List<DishType> findDishTypes(){
       return dishTypeService.getAllDishTypes();
    }*/

    /* 增 */
    @RequestMapping("/dish_add")
    public Result addDish(@RequestBody Dish dish){
        try {
            dishService.addDish(dish);
        } catch (Exception e){
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        log.info("dish--{}",dish);
        return Result.success();
    }
    /* 改 */
    @RequestMapping("/dish_update/{id}")
    public Result dishUpdate(Dish dish){
        dishService.dishUpdate(dish);
        return Result.success();

    }
    /* 删 */
    @RequestMapping("/dishDelete/{id}")
    public Result dishDelete(@PathVariable("id") int id){
        dishService.dishDelete(id);
        return Result.success();
    }

    /*全局查*/
    @RequestMapping("/dish_select")
    public Result selectAll(Dish dish) {
        List<Dish> dishList;
        try{
            dishList = dishService.dishSelect(dish);
        } catch (Exception e){
            if(e instanceof DuplicateKeyException){
                return Result.error("查询数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        log.info("dishList: {}" ,dishList);
        return Result.success(dishList);

    }

/*    *//*分页查*//*
    @RequestMapping("/dish_selectPage")
    public Result getDish(@RequestParam int pageNum,
                          @RequestParam int pageSize,
                          @RequestParam String name ,
                          Dish dish){
        log.info("当前页码："+pageNum);
        log.info("页码大小："+pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        // select * from user where username like '%#{username}%' and name like '%#{name}%'
        Page<User> page = (Page<User>) dishPlusService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
*//*       log.info("查询参数："+dish);
       String dname="";
       if(dish!=null){
           dname=dish.getName();
       }
        int dishCount = dishService.dishCount(dish);
        log.info("查询条数："+dishCount);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pageNum",(pageNum-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("name",dname);

        List<Dish> dishes;
        try {
            dishes = dishService.getDish(map);
        }catch (Exception e){
            if(e instanceof DuplicateKeyException){
                return Result.error("查询数据库错误");
            }else {
                return Result.error("系统错误");
            }
        }
        log.info("响应成功");
        return Result.success(dishes);*//*

*//*
        List<Dish> dishes=dishService.getDish(map);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("菜系如下：\n");
        for (Dish d : dishes) {
            stringBuilder.append(d.toString()).append("\n");
        }
        log.info(stringBuilder.toString());

        return "处理成功";
*//*

    }*/


}
